/**
 * 
 */
package com.ph.member.api.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.ph.base.BaseController;
import com.ph.member.api.controller.qo.MemberInfoQo;
import com.ph.member.api.controller.qo.MemberPageQo;
import com.ph.member.api.service.MemberDataService;
import com.ph.shopping.common.core.constant.PageConstant;
import com.ph.shopping.common.core.customenum.ResultEnum;
import com.ph.shopping.common.util.container.ContainerUtil;
import com.ph.shopping.common.util.date.ThreadLocalDateUtil;
import com.ph.shopping.common.util.excel.ExcelBean;
import com.ph.shopping.common.util.excel.ExcelUtil;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.facade.member.dto.MemberInfoDto;
import com.ph.shopping.facade.member.dto.MemberPageDto;
import com.ph.shopping.facade.member.dto.RegisterMemberDto;
import com.ph.shopping.facade.member.entity.Member;
import com.ph.shopping.facade.member.service.IBankCardBindService;
import com.ph.shopping.facade.member.service.IMemberService;

/**
 * @项目：phshopping-api-platform
 *
 * @描述：
 *
 * @作者： Mr.chang
 *
 * @创建时间：2017年3月9日
 *
 * @Copyright @2017 by Mr.chang
 */
@Controller
@RequestMapping("api/member")
public class MemberController extends BaseController{
	
	private static final Logger log = LoggerFactory.getLogger(MemberController.class);

	@Reference(version = "1.0.0", retries = 0, timeout = 30000)
	private IMemberService memberService;

	@Reference(version = "1.0.0", retries = 0, timeout = 30000)
	private IBankCardBindService bankCardService;

	@Autowired
	private MemberDataService memberDataService;
	/**
	 * 
	* @Title: list  
	* @Description: 获取会员数据列表  
	* @param @return    参数  
	* @return String    返回类型  
	* @throws
	 */
	@RequestMapping("/listPage")
	@ResponseBody
	public Result listPage(HttpServletRequest request, MemberPageQo qo) {
		log.info("加载会员列表参数 MemberPageDto = {} ", JSON.toJSONString(qo));
		Result result = getResult(ResultEnum.MEMBER_NO_DATA);
		try {
			MemberPageDto dto = new MemberPageDto();
			BeanUtils.copyProperties(qo, dto);
			dto.setCreateStartTime(ThreadLocalDateUtil.strToDate("yyyy-MM-dd", qo.getCreateStartTime()));
			dto.setCreateEndTime(ThreadLocalDateUtil.strToDate("yyyy-MM-dd", qo.getCreateEndTime()));
			if (dto.getPageNum() == null) {
				dto.setPageNum(PageConstant.pageNum);
			}
			if (dto.getPageSize() == null) {
				dto.setPageSize(PageConstant.pageSize);
			}
			result = memberService.getMemberListByPage(dto);
		} catch (Exception e) {
			log.error("加载会员数据列表错误", e);
		}
		return result;
	}
	/**
	 * 
	* @Title: listMemberPage  
	* @Description: 加载页面 
	* @param @return    参数  
	* @return String    返回类型  
	* @throws
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		return "/member/list";
	}
	/**
	 * 
	* @Title: addMemberView  
	* @Description:添加会员页面
	* @param @return    参数  
	* @return String    返回类型  
	* @throws
	 */
	@RequestMapping(value = "/addMemberView", method = RequestMethod.GET)
	public String addMemberView() {
		
		return "/member/addMemberView";
	}
	/**
	 * 
	* @Title: addMemberSave  
	* @Description: 添加会员保存操作
	* @param @param request
	* @param @param dto
	* @param @return    参数  
	* @return Map<String,Object>    返回类型  
	* @throws
	 */
	@RequestMapping("/addMemberSave")
	@ResponseBody
	public Map<String, Object> addMemberSave(HttpServletRequest request,MemberInfoQo qo){
		log.info("添加会员参数 MemberInfoQo = {} " ,JSON.toJSONString(qo));
		Map<String, Object> map = ContainerUtil.map();
		map.put("code", ResultEnum.MEMBER_FAILURE.getCode());
		map.put("msg", ResultEnum.MEMBER_FAILURE.getMsg());
		try {
			RegisterMemberDto dto = new RegisterMemberDto();
			Member meber = new Member();
			meber.setMemberName(qo.getMemberName());
			meber.setMemberPhone(qo.getMemberPhone());
			dto.setMeber(meber);
			Result result = memberService.addMember(dto);
			if(result != null && ResultEnum.SUCCESS.getCode().equals(result.getCode())){
				map.put("code", result.getCode());
			}else {
				map.put("msg", result.getMessage());
			}
		} catch (Exception e) {
			log.error("添加会员错误",e);
		}
		return map;
	}
	/**
	 * 
	* @Title: memberFrozen  
	* @Description: 冻结会员 
	* @param @param request
	* @param @param dto
	* @param @return    参数  
	* @return Map<String,Object>    返回类型  
	* @throws
	 */
	@RequestMapping("/memberFrozen")
	@ResponseBody
	public Map<String, Object> memberFrozen(HttpServletRequest request,MemberInfoDto dto){
		log.info("冻结会员参数 MemberPageDto = {} " ,JSON.toJSONString(dto));
		Map<String, Object> map = ContainerUtil.map();
		map.put("code", "300");
		try {
			Result result = memberService.memberFrozen(dto);
			if(result != null && ResultEnum.SUCCESS.getCode().equals(result.getCode())){
				map.put("code", result.getCode());
			}
		} catch (Exception e) {
			log.error("冻结错误",e);
		}
		return map;
	}
	/**
	 * 
	* @Title: relievefrozen  
	* @Description: 解除冻结 
	* @param @param request
	* @param @param dto
	* @param @return    参数  
	* @return Map<String,Object>    返回类型  
	* @throws
	 */
	@RequestMapping("/relievefrozen")
	@ResponseBody
	public Map<String, Object> relievefrozen(HttpServletRequest request, MemberInfoDto dto) {
		log.info("解除冻结会员参数 MemberPageDto = {} ", JSON.toJSONString(dto));
		Map<String, Object> map = ContainerUtil.map();
		map.put("code", "300");
		try {
			Result result = memberService.memberRelievefrozen(dto);
			if (result != null && ResultEnum.SUCCESS.getCode().equals(result.getCode())) {
				map.put("code", result.getCode());
			}
		} catch (Exception e) {
			log.error("解除会员冻结错误", e);
		}
		return map;
	}
	/**
	 * 
	* @Title: exportExcel  
	* @Description: 导出  
	* @param @param request
	* @param @param dto    参数  
	* @return void    返回类型  
	* @throws
	 */
	@RequestMapping("/memberExportExcel")
	public void exportExcel(HttpServletRequest request, HttpServletResponse response, MemberPageQo qo) {
		log.info("导出会员列表参数 MemberPageDto = {} ", JSON.toJSONString(qo));
		boolean flag = false;
		try {
			MemberPageDto dto = new MemberPageDto();
			BeanUtils.copyProperties(qo, dto);
			dto.setCreateStartTime(ThreadLocalDateUtil.strToDate("yyyy-MM-dd", qo.getCreateStartTime()));
			dto.setCreateEndTime(ThreadLocalDateUtil.strToDate("yyyy-MM-dd", qo.getCreateEndTime()));
			Result result = memberService.getMemberList(dto);
			log.info("导出会员列表返回数据 Result = {} ", JSON.toJSONString(result));
			if (result != null && ResultEnum.SUCCESS.getCode().equals(result.getCode())) {
				Object obj = result.getData();
				if (obj instanceof List) {
					@SuppressWarnings("unchecked")
					List<Member> list = (List<Member>) obj;
					ExcelBean exelBean = memberDataService.getMemberExcelBean(list);
					ExcelUtil.download(request, response, exelBean);
					flag = true;
				}
			}
		} catch (Exception e) {
			log.error("导出会员数据列表错误", e);
		}
		if (flag) {
			ExcelUtil.responseMsg(response, "导出会员数据失败");
		}
	}
	
}
